//
//  Complex.cpp
//  myapp
//
//  Created by nilyang on 2021/3/22.
//

#include "Complex.hpp"

//==========={{{==========//
double real(const Complex& ths)
{
    return ths.real();
}

double imag(const Complex& ths)
{
    return ths.imag();
}
std::ostream& operator<<(std::ostream& os, const Complex& rhs)
{
    return os << "(" << real(rhs) << "," << imag(rhs) << ");" << std::endl;
}
//===========}}}==========//

/********{{{*********/

Complex::Complex(double re, double im)
    : real_(re)
    , imag_(im)
{
    std::cout << "默认构造函数 Complex(const Complex& rhs)\n";
}

Complex::Complex(const Complex& rhs)
    : real_(rhs.real_)
    , imag_(rhs.imag_)
{
    std::cout << "拷贝构造函数 Complex(const Complex& rhs)\n";
}

Complex& Complex::operator=(const Complex& rhs)
{
    real_ = rhs.real_;
    imag_ = rhs.imag_;
    std::cout << "赋值构造函数 Complex::operator=(const Complex& rhs)\n";
    return *this;
}

Complex::Complex(Complex&& rhs)
    : real_(rhs.real_)
    , imag_(rhs.imag_)
{
    real_ = rhs.real_;
    imag_ = rhs.imag_;
    std::cout << "移动构造函数 Complex(const Complex&& rhs)\n"
              << rhs;
}

Complex& Complex::operator=(Complex&& rhs)
{
    real_ = rhs.real_;
    imag_ = rhs.imag_;
    std::cout << "移动赋值构造函数 Complex::operator=(Complex&& rhs)\n"
              << rhs;
    return *this;
}

/*********}}}********/

Complex& __doapl(Complex* ths, const Complex& rhs)
{
    ths->real_ += rhs.real_;
    ths->imag_ += rhs.imag_;
    return *ths;
}

// complex += complex

Complex& Complex::operator+=(const Complex& rhs)
{
    return __doapl(this, rhs);
}

// 减法

Complex& __doami(Complex* ths, const Complex& rhs)
{
    ths->real_ -= rhs.real();
    ths->imag_ -= rhs.imag();
    return *ths;
}

// complex -= complex

Complex& Complex::operator-=(const Complex& rhs)
{
    return __doami(this, rhs);
}

// *=

Complex& __doaml(Complex* ths, const Complex& rhs)
{
    double f = ths->real_ * rhs.real_ - ths->imag_ * rhs.imag_;
    ths->imag_ = ths->real_ * rhs.imag_ + ths->imag_ * rhs.real_;
    ths->real_ = f;
    return *ths;
}

Complex& Complex::operator*=(const Complex& rhs)
{
    return __doaml(this, rhs);
}

//-----------{{{--------------//

// complex+complex
Complex operator+(const Complex ths, const Complex rhs)
{
    return Complex(real(ths) + real(rhs),
        imag(ths) + imag(rhs));
}

// complex+number
Complex operator+(const Complex ths, double number)
{
    return Complex(real(ths) + number, imag(ths));
}

// number + complex
Complex operator+(double num, const Complex rhs)
{
    return Complex(num + real(rhs), imag(rhs));
}

// complex-complex

Complex operator-(const Complex& ths, const Complex& rhs)
{
    return Complex(real(ths) - real(rhs), imag(ths) - imag(rhs));
}

//-----------}}}--------------//
